﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace Nomad.WebSync.Windows.Pages {
    /// <summary>
    /// Interaction logic for ContactDetail.xaml
    /// </summary>
    public partial class ContactDetail : Page {
        private DateTime NullDateTime = new DateTime(4501, 1, 1, 0, 0, 0, 0);

        private WebSyncService.Contact contact;

        public ContactDetail() {
            InitializeComponent();
        }

        public ContactDetail(WebSyncService.Contact contact)
            : this() {
            this.contact = contact;

            this.setHeader();
            this.loadListDetails();
        }

        private void setHeader() {
            this.fullNameLabel.Content = String.Empty;
            this.fullNameLabel.Content += this.contact.Title + " ";
            this.fullNameLabel.Content += this.contact.FirstName + " ";
            this.fullNameLabel.Content += this.contact.MiddleName + " ";
            this.fullNameLabel.Content += this.contact.LastName + " ";
            this.fullNameLabel.Content += this.contact.Suffix + " ";
            this.fullNameLabel.Content = this.fullNameLabel.Content.ToString().Trim();

            this.jobTileLabel.Content = this.contact.JobTitle.Trim();
            this.departmentLabel.Content = this.contact.Department.Trim();
            this.companyLabel.Content = this.contact.CompanyName.Trim();

            if (String.IsNullOrEmpty(this.fullNameLabel.Content.ToString()))
                this.fullNameLabel.Visibility = Visibility.Collapsed;
            if (String.IsNullOrEmpty(this.jobTileLabel.Content.ToString()))
                this.jobTileLabel.Visibility = Visibility.Collapsed;
            if (String.IsNullOrEmpty(this.departmentLabel.Content.ToString()))
                this.departmentLabel.Visibility = Visibility.Collapsed;
            if (String.IsNullOrEmpty(this.companyLabel.Content.ToString()))
                this.companyLabel.Visibility = Visibility.Collapsed;

            this.bodyTextBlock.Text = this.contact.Body;
        }
        private void loadListDetails() {
            var propertySource = new Dictionary<string, object>();
            PropertyInfo[] contactProperties = contact.GetType().GetProperties();
            foreach (var property in contactProperties) {
                if (property.CanRead) {
                    var propertyValue = property.GetValue(contact, null);

                    if (String.IsNullOrEmpty(propertyValue.ToString())) continue;
                    try { if (Convert.ToDateTime(propertyValue) == NullDateTime) continue; } catch { }

                    switch (property.Name) {
                        #region Hidden Fields
                        case "ID":
                        case "State":
                        case "oid":
                        case "Body":
                        case "Title":
                        case "FirstName":
                        case "MiddleName":
                        case "LastName":
                        case "Suffix":
                        case "JobTitle":
                        case "Department":
                        case "CompanyName":
                        case "FileAs":
                            break;
                        #endregion

                        #region Address Fields
                        case "BusinessAddressStreet":
                        case "BusinessAddressCity":
                        case "BusinessAddressState":
                        case "BusinessAddressPostalCode":
                        case "BusinessAddressCountry":
                            if (propertySource.SingleOrDefault(key => key.Key == "Work addr.").Value == null) {
                                string workAddr = String.Empty;
                                workAddr += this.contact.BusinessAddressStreet + "\r\n";
                                workAddr += this.contact.BusinessAddressCity + "\r\n";
                                workAddr += this.contact.BusinessAddressState + "\r\n";
                                workAddr += this.contact.BusinessAddressPostalCode + "\r\n";
                                workAddr += this.contact.BusinessAddressCountry;
                                workAddr = workAddr.Trim(' ', '\r', '\n');
                                propertySource.Add("Work addr.", workAddr);
                            }
                            break;

                        case "HomeAddressStreet":
                        case "HomeAddressCity":
                        case "HomeAddressState":
                        case "HomeAddressPostalCode":
                        case "HomeAddressCountry":
                            if (propertySource.SingleOrDefault(key => key.Key == "Home addr.").Value == null) {
                                string homeAddr = String.Empty;
                                homeAddr += this.contact.HomeAddressStreet + "\r\n";
                                homeAddr += this.contact.HomeAddressCity + "\r\n";
                                homeAddr += this.contact.HomeAddressState + "\r\n";
                                homeAddr += this.contact.HomeAddressPostalCode + "\r\n";
                                homeAddr += this.contact.HomeAddressCountry;
                                homeAddr = homeAddr.Trim(' ', '\r', '\n');
                                propertySource.Add("Home addr.", homeAddr);
                            }
                            break;

                        case "OtherAddressStreet":
                        case "OtherAddressCity":
                        case "OtherAddressState":
                        case "OtherAddressPostalCode":
                        case "OtherAddressCountry":
                            if (propertySource.SingleOrDefault(key => key.Key == "Other addr.").Value == null) {
                                string otherAddr = String.Empty;
                                otherAddr += this.contact.OtherAddressStreet + "\r\n";
                                otherAddr += this.contact.OtherAddressCity + "\r\n";
                                otherAddr += this.contact.OtherAddressState + "\r\n";
                                otherAddr += this.contact.OtherAddressPostalCode + "\r\n";
                                otherAddr += this.contact.OtherAddressCountry;
                                otherAddr = otherAddr.Trim(' ', '\r', '\n');
                                propertySource.Add("Other addr.", otherAddr);
                            }
                            break;
                        #endregion

                        #region Date Fields
                        case "Anniversary":
                            var anniversary = Convert.ToDateTime(propertyValue);
                            propertySource.Add(property.Name, anniversary.ToShortDateString());
                            break;
                        case "Birthday":
                            var birthday = Convert.ToDateTime(propertyValue);
                            propertySource.Add(property.Name, birthday.ToShortDateString());
                            break; 
                        #endregion

                        #region Long Name Fields
                        case "WebPage":
                            propertySource.Add("Web page", propertyValue);
                            break;
                        case "RadioTelephoneNumber":
                            propertySource.Add("Radio tel.", propertyValue);
                            break;
                        case "PagerNumber":
                            propertySource.Add("Pager", propertyValue);
                            break;
                        case "OfficeLocation":
                            propertySource.Add("Office loc.", propertyValue);
                            break;
                        case "MobileTelephoneNumber":
                            propertySource.Add("Mobile tel.", propertyValue);
                            break;
                        case "HomeTelephoneNumber":
                            propertySource.Add("Home tel.", propertyValue);
                            break;
                        case "HomeFaxNumber":
                            propertySource.Add("Home fax", propertyValue);
                            break;
                        case "Home2TelephoneNumber":
                            propertySource.Add("Home2 tel.", propertyValue);
                            break;
                        case "Email2Address":
                            propertySource.Add("E-mail2", propertyValue);
                            break;
                        case "Email3Address":
                            propertySource.Add("E-mail3", propertyValue);
                            break;
                        case "Email1Address":
                            propertySource.Add("E-mail", propertyValue);
                            break;
                        case "CarTelephoneNumber":
                            propertySource.Add("Car tel.", propertyValue);
                            break;
                        case "BusinessTelephoneNumber":
                            propertySource.Add("Work tel.", propertyValue);
                            break;
                        case "BusinessFaxNumber":
                            propertySource.Add("Work fax", propertyValue);
                            break;
                        case "Business2TelephoneNumber":
                            propertySource.Add("Work2 tel.", propertyValue);
                            break;
                        case "AssistantTelephoneNumber":
                            propertySource.Add("Assistant tel.", propertyValue);
                            break;
                        case "AssistantName":
                            propertySource.Add("Assistant", propertyValue);
                            break; 
                        #endregion

                        default:
                            propertySource.Add(property.Name, propertyValue);
                            break;
                    }
                }
            }
            this.fieldList.ItemsSource = propertySource;
        }
    }
}
